package com.example.ming.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaParser;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.ming.R;
import com.example.ming.activity.LoginActivity;
import com.example.ming.activity.PlayHistoryActivity;
import com.example.ming.activity.SettingActivity;
import com.example.ming.activity.UserInfoActivity;
import com.example.ming.utils.AnalysisUtils;

public class MyInfoView {

    private final Activity mContext;
    private final LayoutInflater mInflater;
    private View mCurrentView;
    private LinearLayout ll_head;
    private ImageView iv_head_icon;
    private RelativeLayout rl_course_history;
    private RelativeLayout rl_setting;
    private TextView tv_user_name;

    public MyInfoView(Activity context){
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
    }

    private void createView(){
        initView();
    }

    private void initView() {
        mCurrentView = mInflater.inflate(R.layout.main_view_myinfo, null);
        ll_head = (LinearLayout) mCurrentView.findViewById(R.id.ll_head);
        iv_head_icon = (ImageView) mCurrentView.findViewById(R.id.iv_head_icon);
        rl_course_history = (RelativeLayout) mCurrentView.findViewById(R.id.rl_course_history);
        rl_setting = (RelativeLayout) mCurrentView.findViewById(R.id.rl_setting);
        tv_user_name = (TextView) mCurrentView.findViewById(R.id.tv_user_name);
        mCurrentView.setVisibility(View.VISIBLE);
        setLoginParams(readLoginStatus());//设置登陆时界面控件的状态


        ll_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (readLoginStatus()){//判断是否登录，有登录跳转到个人资料页面
                    //Toast.makeText(mContext, "跳转到个人资料页面，尚未完成", Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(mContext, UserInfoActivity.class);
                    mContext.startActivity(intent);
                }else {//未登录就跳转到登录界面
                    Intent intent=new Intent(mContext, LoginActivity.class);
                    mContext.startActivityForResult(intent,1);
                }
            }
        });

        rl_course_history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (readLoginStatus()){
                    //Toast.makeText(mContext, "跳转到播放记录页面，未完成", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(mContext, PlayHistoryActivity.class);
                    mContext.startActivity(intent);
                }else{
                    Toast.makeText(mContext, "你还没登录，请登录", Toast.LENGTH_SHORT).show();
                }
            }
        });

        rl_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (readLoginStatus()){
                    //Toast.makeText(mContext, "跳转到设置页面，未完成", Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(mContext, SettingActivity.class);
                    mContext.startActivityForResult(intent,1);
                }else{
                    Toast.makeText(mContext, "你还没登录，请登录", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void setLoginParams(boolean isLogin) {//登陆成功后设置我的界面
        if (isLogin){
            tv_user_name.setText(AnalysisUtils.readLoginUserName(mContext));
        }else{
            tv_user_name.setText("点击登录");
        }

    }

    private boolean readLoginStatus() {//从SharedPreferences中读取登录状态
        SharedPreferences sp= mContext.getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        boolean isLogin = sp.getBoolean("isLogin", false);
        return isLogin;
    }

    public View getView() {//获取当前在导航栏上方显示对应的view
        if (mCurrentView == null){
            createView();
        }
        return mCurrentView;
    }

    public void showView() {//显示当前导航栏上方所对应的view界面
        if (mCurrentView == null){
            createView();
        }
        mCurrentView.setVisibility(View.VISIBLE);
    }
}
